﻿using System;
using System.Windows.Forms;
using SharpGL;
using System.Drawing;

namespace ModernOpenGLSample
{
    /// <summary>
    /// A form to render the scene.
    /// </summary>
    public partial class FormModernOpenGLSample : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FormModernOpenGLSample"/> class.
        /// </summary>
        public FormModernOpenGLSample()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the OpenGLInitialized event of the openGLControl1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void openGLControl1_OpenGLInitialized(object sender, EventArgs e)
        {
            //  Initialise the scene.
            scene.Initialise(openGLControl1.OpenGL, Width, Height);
        }

        /// <summary>
        /// Handles the OpenGLDraw event of the openGLControl1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="RenderEventArgs"/> instance containing the event data.</param>
        private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs args)
        {
            //  Draw the scene.
            scene.Draw(openGLControl1.OpenGL, openGLControl1.Width, openGLControl1.Height);
        }

        /// <summary>
        /// The scene that we are rendering.
        /// </summary>
        private readonly Scene scene = new Scene();
        bool key_w, key_s, key_a, key_d,key_shift,key_space;
        bool mouseDown;
        double x, y;
        private void openGLControl1_MouseDown(object sender, MouseEventArgs e) {
            mouseDown = true;
            x = e.X / Settings.sensibility;
            y = e.Y / Settings.sensibility;
        }

        private void openGLControl1_MouseMove(object sender, MouseEventArgs e) {
            Util.mouseX = e.X;
            Util.mouseY = ((OpenGLControl)sender).Height - e.Y ;
            if (mouseDown) {
                Camera.xa -= x - e.X / Settings.sensibility;
                Camera.ya -= y - e.Y / Settings.sensibility;
                x = e.X / Settings.sensibility;
                y = e.Y / Settings.sensibility;
            }
        }

        private void 设置ToolStripMenuItem_Click(object sender, EventArgs e) {
            SettingsForm settingsForm = new SettingsForm();
            settingsForm.Show();
        }

        private void openGLControl1_Load_1(object sender, EventArgs e) {

        }

        private void pictureBox1_Click(object sender, EventArgs e) {
        }

        private void openGLControl1_MouseUp(object sender, MouseEventArgs e) {
            mouseDown = false;
        }

        private void openGLControl1_Load(object sender, EventArgs e) {

        }

        private void FormModernOpenGLSample_Load(object sender, EventArgs e) {

        }
         
        private void timer1_Tick(object sender, EventArgs e) {
            try {
                pictureBox1.Image = scene.lightSettingBitmap;
            } catch (Exception exp) {
            }
            Scene.time += 1;
            if (key_a) {
                Camera.x += Settings.movement * Math.Cos(Camera.xa);
                Camera.z -= Settings.movement * Math.Sin(Camera.xa);
            }
            if (key_d) {
                Camera.x -= Settings.movement * Math.Cos(Camera.xa);
                Camera.z += Settings.movement * Math.Sin(Camera.xa);
            }
            if (key_w) {
                Camera.z += Settings.movement * Math.Cos(Camera.xa) * Math.Cos(Camera.ya);
                Camera.x += Settings.movement * Math.Sin(Camera.xa);
                Camera.y -= Settings.movement * Math.Sin(Camera.ya);
            }
            if (key_s) {
                Camera.z -= Settings.movement * Math.Cos(Camera.xa) * Math.Cos(Camera.ya);
                Camera.x -= Settings.movement * Math.Sin(Camera.xa);
                Camera.y += Settings.movement * Math.Sin(Camera.ya);
            }
            if (key_shift) {
                Camera.y += Settings.movement;
            }
            if (key_space) {
                Camera.y -= Settings.movement;
            }

        }

        private void FormModernOpenGLSample_KeyDown(object sender, KeyEventArgs e) {
            switch (e.KeyCode) {
                case Keys.A:
                    key_a = true;
                    break;
                case Keys.W:
                    key_w = true;
                    break;
                case Keys.S:
                    key_s = true;
                    break;
                case Keys.D:
                    key_d = true;
                    break;
                case Keys.ShiftKey:
                    key_shift = true;
                    break;
                case Keys.Space:
                    key_space = true;
                    break;
                default:
                    break;
            }
        }

        private void FormModernOpenGLSample_KeyUp(object sender, KeyEventArgs e) {
            switch (e.KeyCode) {
                case Keys.A:
                    key_a = false;
                    break;
                case Keys.W:
                    key_w = false;
                    break;
                case Keys.S:
                    key_s = false;
                    break;
                case Keys.D:
                    key_d = false;
                    break;
                case Keys.ShiftKey:
                    key_shift = false;
                    break;
                case Keys.Space:
                    key_space = false;
                    break;
                default:
                    break;
            }
        }
    }
}
